<?php

/*
  PHP version 5
  Copyright (c) 2002-2010 ECISP.CN
  声明：这不是一个免费的软件，请在许可范围内使用

  作者：Bili E-mail:huangqyun@163.com  QQ:6326420
  http://www.ecisp.cn	http://www.easysitepm.com
 */

class mainpage extends connector {

	function mainpage() {
		$this->softbase(false);
	}

	function in_list() {
		parent::start_pagetemplate();
		include_once admin_ROOT . 'public/class_pagebotton.php';
		$lng = (admin_LNG == 'big5') ? $this->CON['is_lancode'] : admin_LNG;

		$page = $this->fun->accept('page', 'R');
		$page = !empty($page) ? intval($page) : 1;

		$tid = intval($this->fun->accept('tid', 'R'));
		if (empty($tid)) {
			return false;
		}
		$typeread = $this->get_type($tid);
		if (!$typeread['isclass']) {
			exit("Information parameter error!");
		} elseif ($typeread['purview'] > 0) {
			parent::member_purview($typeread['purview'], null, true);
		}

		$current = !$typeread['upid'] ? $typeread['tid'] : $typeread['topid'];
		$this->pagetemplate->assign('path', 'article');
		$this->pagetemplate->assign('current', $current);

		$mid = $typeread['mid'];
		$modelview = $this->get_modelview($mid);
		
		$modelname = $modelview['modelname'];

		$pagemax = $modelview['pagemax'];
		$pagemax = empty($pagemax) ? 20 : $pagemax;

		$pagesylte = $modelview['pagesylte'];

		$isorder = $modelview['isorder'];
		
		$isexhibit = $modelview['isexhibit'];

		$istime = $modelview['istime'];

		$template = $typeread['template'];

		$readtemplate = $typeread['readtemplate'];

		$styleid = $typeread['styleid'];
		$typeread['rsslink'] = $this->get_link('typerss', $typeread, admin_LNG);
		$this->pagetemplate->assign('type', $typeread);

		if ($styleid != 4) {
			if (!empty($typeread['keywords'])) {
				$this->lng['keyword'] = $typeread['keywords'];
			}
			if (!empty($typeread['description'])) {
				$this->lng['description'] = $typeread['description'];
			}
			$this->pagetemplate->assign('lngpack', $this->lng);
		}
		
		$templatesDIR = $this->get_templatesdir('article');
		
		$templatefilename = $lng . '/' . $templatesDIR . '/' . $template;
		
		$readtemplatefilename = $lng . '/' . $templatesDIR . '/' . $readtemplate;
		if ($styleid == 1) {
			unset($typeread, $modelview, $LANPACK, $this->lng);
			$this->pagetemplate->display($templatefilename, $dirname . '_index', false, $filename, admin_LNG);
		} elseif ($styleid == 2) {
			$db_table = db_prefix . 'document';
			if ($typeread['ispart']) {
				$db_where = ' WHERE lng=\'' . $lng . '\' AND isclass=1 AND mid=' . $mid . ' AND ' . $this->get_typeid($tid, 'tid', 0, $mid, 0, $lng) . " OR FIND_IN_SET('$tid',extid)";
			} else {
				$db_where = ' WHERE lng=\'' . $lng . '\' AND isclass=1 AND mid=' . $mid . ' AND tid=' . $tid . " OR FIND_IN_SET('$tid',extid)";
			}
			$countnum = $this->db_numrows($db_table, $db_where);
			if ($countnum > 0) {

				$numpage = ceil($countnum / $pagemax);
			} else {
				$numpage = 1;
			}
			$db_table = db_prefix . 'document';
			$sql = "SELECT did,lng,pid,mid,aid,tid,extid,sid,fgid,linkdid,isclass,islink,ishtml,ismess,isorder,purview,recommend,tsn,title,longtitle,
			color,author,source,pic,link,oprice,bprice,click,addtime,template,keywords,description,filename,filepath FROM $db_table $db_where LIMIT 0,$pagemax";
			$this->htmlpage = new PageBotton($sql, $pagemax, $page, $countnum, $numpage, $pagesylte, $this->CON['file_fileex'], 5, $this->lng['pagebotton'], $this->lng['gopageurl'], $this->CON['is_rewrite']);
			$sql = $this->htmlpage->PageSQL('pid,did', 'down');
			$rs = $this->db->query($sql);
			while ($rsList = $this->db->fetch_assoc($rs)) {
				$attarray = $exarray = array();
				$rsList['value'] = $this->get_document_value($rsList['did'], 'value');
				if (!empty($rsList['value'])) {
					$attarray = unserialize($rsList['value']);
					unset($rsList['value']);
				}
				$typeread = $this->get_type($rsList['tid']);
				$rsList['typename'] = $typeread['typename'];
				$rsList['typelink'] = $this->get_link('type', $typeread, admin_LNG);
				
				$rsList['link'] = $this->get_link('doc', $rsList, admin_LNG);
				$rsList['buylink'] = $this->get_link('buylink', $rsList, admin_LNG);
				$rsList['enqlink'] = $this->get_link('enqlink', $rsList, admin_LNG);
				$rsList['ctitle'] = empty($rsList['color']) ? $rsList['title'] : "<font color='" . $rsList['color'] . "'>" . $rsList['title'] . "</font>";
				if($isexhibit==1) {
					$exarray = $this->get_document_exhibit($rsList['did']);
					$exarray['cityname'] = $this->get_cityview($exarray['city'],'cityname');
					$rsList['cityname'] = $exarray['cityname'];
					$rsList['startdate'] = $exarray['startdate'];
					$rsList['enddate'] = $exarray['enddate'];
				}
				$array[] = is_array($attarray) ? array_merge_recursive($rsList, $attarray) : $rsList;
			}
			
			$this->pagetemplate->assign('pagetext', $this->htmlpage->PageStat($this->lng['pagetext']));
			$this->pagetemplate->assign('pagebotton', $this->htmlpage->PageList());
			$this->pagetemplate->assign('pagenu', $this->htmlpage->Bottonstyle(false));
			$this->pagetemplate->assign('pagese', $this->htmlpage->pageSelect());
			$this->pagetemplate->assign('pagevt', $this->htmlpage->Prevbotton());

			$this->pagetemplate->assign('array', $array);
			unset($array, $typeread, $modelview, $LANPACK, $this->lng);
			$this->pagetemplate->display($templatefilename, $dirname . '_list', false, $filename, admin_LNG);
		} elseif ($styleid == 3) {
			$typeurl = $typeread['typeurl'];
			header("location:$typeurl");
			exit();
		} elseif ($styleid == 4) {
			$db_table = db_prefix . 'document';
			if ($typeread['ispart']) {
				$db_sql = "SELECT did FROM $db_table WHERE isclass=1 AND mid=$mid AND tid=$tid OR FIND_IN_SET('$tid',extid) ORDER BY pid,did DESC LIMIT 0,1";
			} else {
				$db_sql = "SELECT did FROM $db_table WHERE isclass=1 AND mid=$mid AND tid=$tid ORDER BY pid,did DESC LIMIT 0,1";
			}
			$readdid = $this->db->fetch_first($db_sql);
			if ($readdid) {
				$readinfo = $this->get_document($readdid['did']);
				$readinfo['content'] = html_entity_decode($readinfo['content']);

				if (!empty($readinfo['keywords'])) {
					$this->lng['keyword'] = $readinfo['keywords'];
				}
				if (!empty($readinfo['description'])) {
					$this->lng['description'] = $readinfo['description'];
				}
				$this->pagetemplate->assign('lngpack', $this->lng);

				$readinfo['buylink'] = $this->get_link('buylink', $readinfo, admin_LNG);
				$readinfo['enqlink'] = $this->get_link('enqlink', $readinfo, admin_LNG);

				if (!empty($readinfo['linkdid'])) {
					$readinfo['linkdid'] = str_replace(',', '/', $readinfo['linkdid']);
				}

				$albumarray = $this->get_album_array($readinfo['did']);

				$docarray = array();
				if (!empty($readinfo['value'])) {
					$attarray = unserialize($readinfo['value']);
					unset($readinfo['value']);
					$docarray = is_array($attarray) ? array_merge_recursive($readinfo, $attarray) : $readinfo;
				} else {
					$docarray = $readinfo;
				}
				unset($readinfo);
				$this->pagetemplate->assign('read', $docarray);
				$this->pagetemplate->assign('photo', $albumarray);
			}
			unset($docarray, $typeread, $modelview, $LANPACK, $this->lng);
			$this->pagetemplate->display($readtemplatefilename, $dirname . '_list', false, $filename, admin_LNG);
		}
	}

	function in_read() {
		$this->start_pagetemplate();
		$lng = (admin_LNG == 'big5') ? $this->CON['is_lancode'] : admin_LNG;
		$did = intval($this->fun->accept('did', 'G'));
		if (empty($did)) {
			$this->callmessage($this->lng['db_err'], $_SERVER['HTTP_REFERER'], $this->lng['gobackurlbotton']);
		}
		$db_table = db_prefix . 'document';

		$readinfo = $this->get_document($did);
		if (!$readinfo['isclass']) {
			exit("Information parameter error!");
		}

		$db_where = "isclass=1 AND did=$did";
		$db_set = "click=click+1";
		$this->db->query('UPDATE ' . $db_table . ' SET ' . $db_set . ' WHERE ' . $db_where);

		if ($readinfo['islink']) {
			$urladd = $readinfo['link'];
			header("location:$urladd");
			exit();
		}

		if (!empty($readinfo['tags'])) {
			$tagArray = explode(',', $readinfo['tags']);
			$tagArray = array_unique($tagArray);
			$newTagArray = array();
			foreach ($tagArray as $key => $value) {
				$newTagArray[$key]['title'] = $value;
				$newTagArray[$key]['link'] = $this->get_link('taglink', array('key' => $value), admin_LNG);
			}
		}
		$readinfo['content'] = html_entity_decode($readinfo['content']);

		if (!empty($readinfo['keywords'])) {
			$this->lng['keyword'] = $readinfo['keywords'];
		}
		if (!empty($readinfo['description'])) {
			$this->lng['description'] = $readinfo['description'];
		}
		$this->pagetemplate->assign('lngpack', $this->lng);
		$readinfo['buylink'] = $this->get_link('buylink', $readinfo, admin_LNG);
		$readinfo['enqlink'] = $this->get_link('enqlink', $readinfo, admin_LNG);
		$typeview = $this->get_type($readinfo['tid']);
		if (!$typeview['isclass']) {
			exit("Information parameter error!");
		} elseif ($typeview['purview'] > 0) {
			parent::member_purview($typeread['purview'], null, true);
		} elseif ($readinfo['purview'] > 0) {
			parent::member_purview($readinfo['purview'], null, true);
		}

		$read_templates = ($readinfo['istemplates'] && !empty($readinfo['template'])) ? $readinfo['template'] : $typeview['readtemplate'];
		$dirname = $typeview['dirname'];

		if (!empty($readinfo['linkdid'])) {
			$readinfo['linkdid'] = str_replace(',', '/', $readinfo['linkdid']);
		}
		
		$albumarray = $this->get_album_array($did);

		$docarray = array();
		if (!empty($readinfo['value'])) {
			$attarray = unserialize($readinfo['value']);
			unset($readinfo['value']);
			$docarray = is_array($attarray) ? array_merge_recursive($readinfo, $attarray) : $readinfo;
		} else {
			$docarray = $readinfo;
		}
		unset($readinfo);
		if($docarray['mid'] == 9) {
			$arrexhi = $this->get_exhibition($docarray['industry']);
			$docarray['heldareas_text'] = $docarray['heldareas'];//
			$docarray['industry_text'] = $arrexhi['industry'];
			if($docarray['city']) {
				$docarray['cityname'] = $this->get_cityview($docarray['city'],'cityname');
			}
			$citylist = array();
			include_once admin_ROOT . '/datacache/citylist.php';
			foreach ($citylist as $key=>$city) {
				if($city['Name'] == $docarray['cityname']) {
					$docarray['citycode'] = $city['Id'];
					break;
				}
			}
			$arrlat = explode(',',$docarray['lnglat']);
			//$tm1 = $_COOKIE['search_tm1']; if(!$tm1) { $tm1 = date('Y-m-d',(strtotime(date('Y-m-d'))+24*3600)); setcookie("search_tm1",$tm1,0); }
			//$tm2 = $_COOKIE['search_tm2']; if(!$tm2) { $tm2 = date('Y-m-d',(strtotime(date('Y-m-d'))+24*3600*2)); setcookie("search_tm2",$tm2,0); }
			$param = array(
				'cityCode' => $docarray['citycode'],
				'lng' => $arrlat[0],
				'lat' => $arrlat[1],
				'landmark' => $docarray['heldareas_text'],
				'includeNonBooking' => true,
				'includeGuaranteeable' => true
			);
			$docarray['heldarealinkurl'] = $this->get_link('hotel_nearby',$param,$lng);
		}

		$templatesDIR = $this->get_templatesdir('article');

		$templatefilename = $lng . '/' . $templatesDIR . '/' . $read_templates;

		$current = !$typeview['upid'] ? $typeview['tid'] : $typeview['topid'];
		$this->pagetemplate->assign('path', 'article');
		$this->pagetemplate->assign('current', $current);
		$this->pagetemplate->assign('tagarray', $newTagArray);
		$this->pagetemplate->assign('type', $typeview);
		$this->pagetemplate->assign('read', $docarray);
		$this->pagetemplate->assign('photo', $albumarray['list']);
		unset($typeview, $docarray, $albumarray, $LANPACK, $this->lng);
		$this->pagetemplate->display($templatefilename, $dirname . '_read', false, $filename, admin_LNG);
	}
	
	function in_comment() {
		$this->start_pagetemplate();
		$lng = (admin_LNG == 'big5') ? $this->CON['is_lancode'] : admin_LNG;
		$hid = $this->fun->accept('hid', 'R');
		$hname = $this->fun->accept('hname', 'R');
		$cityid = $this->fun->accept('cityid', 'R');
		$citythree = $this->fun->accept('citythree', 'R');
		$uid = intval($this->fun->accept('uid', 'R'));
		$username = $this->fun->accept('username', 'R');
		$page = $this->fun->accept('page', 'R');
		$page = !empty($page) ? intval($page) : 1;
		if($citythree) { $cityid =$citythree;}
		
		$param = array(
			'uid' => $uid,
			'username' => $username,
			'hid' => $hid,
			'hname' => $hname,
			'cityid' => $cityid,
			'page' => $page,
		);
		
		$pagesize = 10;
		$start = ($page-1)*$pagesize;
		$pagemax = $pagesize;
		
		$lng = empty($lng) ? $this->CON['is_lancode'] : $lng;
		$db_where = ' WHERE 1 '; $cachekey = "";
		if($uid) {
			$db_where .= " and uid=$uid ";
			$cachekey .= "_".$uid;
		} else{
			if($username) {
				$db_where .= " and username like '%$username%' ";
				$cachekey .= "_".urlencode($username);
			}
		}
		if($hid) {
			$db_where .= " and hid='$hid' ";
			$cachekey .= "_".$hid;
		} else {
			if($hname) {
				$db_where .= " and hname like '%$hname%' ";
				$cachekey .= "_".urlencode($hname);
			}
		}
		if(!$hid) {
			if($cityid) {
				$db_where .= " and cityid=$cityid ";
				$cachekey .= "_".$cityid;
			}
		}
		$db_table = db_prefix . 'hotel_comment';
		$sql = "SELECT cid,uid,username,hid,hname,datetime,level,comment FROM $db_table $db_where ORDER BY `cid` DESC LIMIT $start , $pagesize";
		$countnum = $this->db_numrows($db_table, $db_where);
		if ($countnum > 0) {
			$numpage = ceil($countnum / $pagemax);
		} else {
			$numpage = 1;
		}
		
		$chacherray = $this->dbcache->checkcache('hotel_commentlist_array_' . $lng . $cachekey . '_' . $page, false);
		$arrayList = array();
		if (!$chacherray) {
			$array = array();
			$rs = $this->db->query($sql);
			while ($rsList = $this->db->fetch_assoc($rs)) {
				$showurl = $this->get_link("hotelshow",array('hid'=>$rsList['hid']),$lng);
				$rsList['showurl'] = $showurl;
				$rsList['date'] = date('Y-m-d H:i:s',$rsList['datetime']);
				$array[] = $rsList;
			}
			$chacherray = $this->dbcache->cachesave('hotel_commentlist_array_' . $lng . $cachekey . '_' . $page, $array);
			$chacherray = $chacherray ? $chacherray : $array;
			unset($array);
		}
		
		$hotsql = "SELECT hid,hname FROM $db_table ORDER BY cid DESC LIMIT 0, 10";	
		
		$arr = array();
		if(!$hid) {
			$rs = $this->db->query($sql);
			while ($rsComment = $this->db->fetch_assoc($rs)) {
				$showurl = $this->get_link("hotelshow",array('hid'=>$rsComment['hid']),$lng);
				$rsComment['showurl'] = $showurl;
				$arr[] = $rsComment;
			}
		}
		$commentsave = $this->get_link('comment_save',array(),admin_LNG);
		
		$templatesDIR = $this->get_templatesdir('hotel');
		$templatefilename = $templatecache = "";
		if($hid) {
			$templatefilename = $lng . '/' . $templatesDIR . '/comment';
			$templatecache = "comment";
		} else {
			$templatefilename = $lng . '/' . $templatesDIR . '/hotel_comment';
			$templatecache = "hotel_comment";
		}
		if($hid) {
			$this->pagetemplate->assign('commentsave', $commentsave);
		}
		$this->pagetemplate->assign('array', $chacherray);
		if(!$hid) {
			$this->pagetemplate->assign('arr', $arr);
		}
		
		if($numpage == 1) {
			$pagenext = 1;
			$pagepre  = 1;
		} else {
			if($page < $numpage) {
				$pagenext = intval($page) + 1;
				if($page == 1) {
					$pagepre = 1;
				} else {
					$pagepre = intval($page)-1;
				}
			} else {
				$pagenext = $numpage;
				$pagepre = intval($page)-1;
			}
		}
		
		$this->pagetemplate->assign('countnum', $countnum);
		$this->pagetemplate->assign('numpage', $numpage);
		$this->pagetemplate->assign('page', $page);
		
		$this->pagetemplate->assign('pagetext', $this->get_page('comment',$numpage,$page,$param,$lng));   //分页页码
		$paramindex =  $param; $paramindex['page'] = 1; 
		$this->pagetemplate->assign('pageindex', $this->get_link('hotel_comment',$paramindex,$lng));  //首页
		$paramtotal =  $param; $paramtotal['page'] = $numpage; 
		$this->pagetemplate->assign('pagetotal', $this->get_link('hotel_comment',$paramtotal,$lng));  //最后一页
		$parampre =  $param; $parampre['page'] = $pagepre; 
		$this->pagetemplate->assign('pagepre', $this->get_link('hotel_comment',$parampre,$lng));    //前一页
		$paramnext =  $param; $paramnext['page'] = $pagenext; 
		$this->pagetemplate->assign('pagenext', $this->get_link('hotel_comment',$paramnext,$lng));   //最后一页
		
		$this->pagetemplate->assign('hname', $hname);
		$this->pagetemplate->assign('hid', $hid);
		$this->pagetemplate->assign('cityid', $cityid);
		unset($array,$paramindex,$paramtotal,$parampre,$paramnext);
		
		$this->pagetemplate->display($templatefilename, $templatecache, false, '', admin_LNG);
	}
	
	function in_commentsave() {
		
		$check = parent::member_purview_ajax(1, null, true);
		if (empty($this->ec_member_username_id) && empty($this->ec_member_username)) {
			if (!$this->CON['is_anonymous']) { //是否允许匿名留言
				echo "-2";
				exit;
			}
		}
		$username = $this->ec_member_username;
		$uid = $this->ec_member_username_id;
		if(!$uid || !$username) {
			echo "-2";
			exit;
		}
		$hid = $this->fun->accept('hid', 'R');
		$hname = $this->fun->accept('hname', 'R');
		$cityid = $this->fun->accept('cityid', 'R');
		$datetime = time();
		$level = $this->fun->accept('level', 'R');
		$comment = $this->fun->accept('comment', 'R');
		
		$param = array(
			'hid'      => $hid,
			'uid'      => $uid,
			'cityid'   => $cityid,
			'hname'    => $hname,
			'username' => $username,
			'level'    => $level,
			'comment'  => urldecode($comment),
			'datetime' => $datetime
		);

		$db_table = db_prefix . 'hotel_comment';
		$cid = $this->inserttable($db_table, $param, 1);
		if($cid > 0) {
			$this->dbcache->clearcache("hotel_commentlist",true);
			echo $cid;
		} else {
			echo "-1";
		}
		exit;
	}

}
?>